<?php

namespace app\shop\controller\company;
use app\shop\controller\Controller;
use app\shop\service\company\Department as DepartmentService;
use app\shop\model\company\Department as DepartmentModel;
use app\shop\service\company\Staff;
/**
 * 部门管理
 */
class Department extends Controller
{
    /**
     * 部门列表
     */
    public function index(DepartmentService $DepartmentService)
    {
        $app_id = $this->store['app']['app_id'];
        $data = input();
        $data['app_id'] = $app_id;
        return $DepartmentService->lists($data);
    }

    public function options(DepartmentService $DepartmentService)
    {
        $app_id = $this->store['app']['app_id'];
        return $DepartmentService->options($app_id);
    }

    public function add(DepartmentService $DepartmentService)
    {
        try {
            $app_id = $this->store['app']['app_id'];
            $data = input();
            $data['app_id'] = $app_id;
            $department = DepartmentModel::create($data);
            if ($data['manager'] > 0) {
                $Staff = new Staff();
                $set_data = [
                    'user_id' => $data['manager'],
                    'department_id' => $department['id'],
                    'type' => 2
                ];
                $Staff->setStaff($set_data);     //设置部门经理
            }
            return $this->renderSuccess("");
        } catch (\Exception $e) {
            return $this->renderError($e->getMessage());
        }
    }

    public function form()
    {
        $id = input("id");
        $app_id = $this->store['app']['app_id'];
        $info = DepartmentModel::where('app_id', $app_id)->field('id,title,pid,manager,sort')->find($id);
        if (!$info) {
            return $this->renderError('数据不存在');
        }
        return $this->renderSuccess("", $info);
    }

    public function edit(DepartmentService $DepartmentService)
    {
        try {
            $app_id = $this->store['app']['app_id'];
            $data = input();
            //pid不能是自己或自己的下级
            $pid = $data['pid'];
            $id = $data['id'];
            if ($pid == $id) {
                return $this->renderError('上级部门不能选择自己');
            }
            $department = DepartmentModel::where('app_id', $app_id)->find($id);
            if (!$department) {
                return $this->renderError('数据不存在');
            }
            $child_id = $DepartmentService->getChildIds($id);
            if (in_array($pid, $child_id)) {
                return $this->renderError('上级部门不能选择自己的下级');
            }
            $department->save($data);
            $Staff = new Staff();
            if ($data['manager'] > 0) {
                $set_data = [
                    'user_id' => $data['manager'],
                    'department_id' => $department['id'],
                    'type' => 2
                ];
                $Staff->setStaff($set_data);     //设置部门经理
            } else {
                //取消部门经理
                $set_data = [
                    'user_id' => $department['manager'],
                    'department_id' => $department['id'],
                    'type' => 0
                ];
                $Staff->setStaff($set_data);
            }
            return $this->renderSuccess("");
        } catch (\Exception $e) {
            return $this->renderError($e->getMessage());
        }
    }

    public function delete()
    {
        try {
            $id = input("id");
            $app_id = $this->store['app']['app_id'];
            $info = DepartmentModel::where('app_id', $app_id)->find($id);
            if (!$info) {
                return $this->renderError('数据不存在');
            }
            $info->delete();
            return $this->renderSuccess("");
        } catch (\Exception $e) {
            return $this->renderError($e->getMessage());
        }
    }
}